﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;

public partial class changePass : System.Web.UI.Page
{
    //inject EmployeeBLL
    private IEmployeeBLL empBLL = Factory<IEmployeeBLL>.CreateInstance();
    //Inject log4net
    private static readonly ILog logger = LogManager.GetLogger(typeof(changePass).Name);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Inits();
        }
    }

    private void Inits()
    {
        //register javascript
        btnReset.Attributes.Add("onmouseover", "this.src='images/xoa_hover.gif'");
        btnReset.Attributes.Add("onmouseout", "this.src='images/xoa.gif'");
        btnSave.Attributes.Add("onmouseover", "this.src='images/luu_hover.gif'");
        btnSave.Attributes.Add("onmouseout", "this.src='images/luu.gif'");
    }

    /// <summary>
    /// ChangePassword
    /// </summary>
    private void ChangePassword()
    {
        try
        {
            //Old password is right
            if (!CheckOldPassWord())
            {
                //get Employee from session
                Employee emp = new Employee();
                emp = Utils.GetEmployeeSession();
                emp.Password = txtNewPass.Text.Trim();
                //Change pass
                if (empBLL.ChangePassword(emp))
                    lblMsg.Text = "Đổi mật khẩu thành công.";
            }
            
        }
        catch (Exception ex)
        {
           lblMsg.Text = Constants.ERROR_MSG;
           if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }

    /// <summary>
    /// CheckOldPassWord
    /// </summary>
    private bool CheckOldPassWord()
    {
        bool flag = false;
        //get Employee from session
        Employee emp = new Employee();
        emp = Utils.GetEmployeeSession();
        emp.Password = txtOldPass.Text.Trim();
        try
        {
            //Old password is wrong
            if (!empBLL.CheckOldPassWord(emp))
            {
                lblMsg.Text = "Mật khẩu cũ không đúng!";
                flag = true;
            }
            else //Old pass is right
                lblMsg.Text = "";
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
        return flag;
    }


    #region Events
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        ChangePassword();
    }
    protected void btnReset_Click(object sender, ImageClickEventArgs e)
    {
        txtOldPass.Text = "";
        txtNewPass.Text = "";
        txtConfirm.Text = "";
    } 
    
   
    #endregion

    protected const string PasswordMask = "*********************************************************************************************************************************";

}
